#! /bin/bash

# kernel optional !!!
id_b_bin=0
id_r_bin=0
koi_enum=0
koid=0 # kernel optional id
layer_param_ko_num=0 
for ID_B_BIN in $optional_parameter
do
	id_r_bin=0
	for ID_R_BIN in $optional_type_parameter
	do 
		if [ $id_b_bin == $id_r_bin ]
		then
			if [ "$version" == "C30" ];then # C30
				if [ "$ID_R_BIN" == "bool" ];then
					var_line_num_ori=$(grep -rn "optional $ID_R_BIN $ID_B_BIN " $current_path/common/file.txt | awk '{print $1}')
					var_line_num=${var_line_num_ori%:*}
					var_full=`sed -n "$var_line_num"p $current_path/common/file.txt` # get the sentence of $ID_B from caffe.proto
					sed -n "$var_line_num"p $current_path/common/file.txt > $current_path/common/"$ID_B_BIN".txt # get the sentence file of $ID_B from caffe.proto
					if [ `grep -c "default" $current_path/common/"$ID_B_BIN".txt` -eq '0' ];then
							var_cut_bracket=false
					else
							var_cut_default=${var_full#*"default"}  # cut "default"
							var_cut_equal=${var_cut_default#*"="}   # cut "="
							var_cut_bracket_origin=${var_cut_equal%]*}     # cut "]"
							var_cut_left_blank=${var_cut_bracket_origin#*" "}     # cut left blank
							var_cut_right_blank=${var_cut_left_blank%*}     # cut right blank
							var_cut_bracket=$var_cut_right_blank # rename the final default parameter type as var_cut_bracket 
					fi
					rm -rf $current_path/common/"$ID_B_BIN".txt
					sed -i "$((plugin_kernel_optional_offset_C30+poi+prid+boid+brid)) s/^/$(echo "\
	$ID_R_BIN $ID_B_BIN = $var_cut_bracket;")\n/"                                                                                 $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+1)) s/^/$(echo "\
	ge::AttrValue "$ID_B_BIN"AttrValue;")\n/"                                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+2)) s/^/$(echo "\
	if ((ge::GRAPH_SUCCESS != op.GetAttr(\""$ID_B_BIN"\", "$ID_B_BIN"AttrValue))")\n/"                                            $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+3)) s/^/$(echo "\
		|| (ge::GRAPH_SUCCESS != "$ID_B_BIN"AttrValue.GetValue<ge::AttrValue::BOOL>("$ID_B_BIN")))")\n/"                            $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+4)) s/^/$(echo "\
	{")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+5)) s/^/$(echo "\
		printf(\"Can not GetOpAttr "$ID_B_BIN"!\\\n\");")\n/"                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+6)) s/^/$(echo "\
	}")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
				elif [ "$ID_R_BIN" == "float" ];then
					var_line_num_ori=$(grep -rn "optional $ID_R_BIN $ID_B_BIN " $current_path/common/file.txt | awk '{print $1}')
					var_line_num=${var_line_num_ori%:*}
					var_full=`sed -n "$var_line_num"p $current_path/common/file.txt` # get the sentence of $ID_B from caffe.proto
					sed -n "$var_line_num"p $current_path/common/file.txt > $current_path/common/"$ID_B_BIN".txt # get the sentence file of $ID_B from caffe.proto
					if [ `grep -c "default" $current_path/common/"$ID_B_BIN".txt` -eq '0' ];then
							var_cut_bracket=0.0
					else
							var_cut_default=${var_full#*"default"}  # cut "default"
							var_cut_equal=${var_cut_default#*"="}   # cut "="
							var_cut_bracket_origin=${var_cut_equal%]*}     # cut "]"
							var_cut_left_blank=${var_cut_bracket_origin#*" "}     # cut left blank
							var_cut_right_blank=${var_cut_left_blank%*}     # cut right blank
							var_cut_bracket=$var_cut_right_blank # rename the final default parameter type as var_cut_bracket 
					fi
					rm -rf $current_path/common/"$ID_B_BIN".txt
					sed -i "$((plugin_kernel_optional_offset_C30+poi+prid+boid+brid)) s/^/$(echo "\
	$ID_R_BIN $ID_B_BIN = $var_cut_bracket;")\n/"                                                                                 $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+1)) s/^/$(echo "\
	ge::AttrValue "$ID_B_BIN"AttrValue;")\n/"                                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+2)) s/^/$(echo "\
	if ((ge::GRAPH_SUCCESS != op.GetAttr(\""$ID_B_BIN"\", "$ID_B_BIN"AttrValue))")\n/"                                            $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+3)) s/^/$(echo "\
		|| (ge::GRAPH_SUCCESS != "$ID_B_BIN"AttrValue.GetValue<ge::AttrValue::FLOAT>("$ID_B_BIN")))")\n/"                           $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+4)) s/^/$(echo "\
	{")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+5)) s/^/$(echo "\
		printf(\"Can not GetOpAttr "$ID_B_BIN"!\\\n\");")\n/"                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+6)) s/^/$(echo "\
	}")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
				elif [ "$ID_R_BIN" == "string" ];then
					var_line_num_ori=$(grep -rn "optional $ID_R_BIN $ID_B_BIN " $current_path/common/file.txt | awk '{print $1}')
					var_line_num=${var_line_num_ori%:*}
					var_full=`sed -n "$var_line_num"p $current_path/common/file.txt` # get the sentence of $ID_B from caffe.proto
					sed -n "$var_line_num"p $current_path/common/file.txt > $current_path/common/"$ID_B_BIN".txt # get the sentence file of $ID_B from caffe.proto
					if [ `grep -c "default" $current_path/common/"$ID_B_BIN".txt` -eq '0' ];then
							var_cut_bracket=\"""\"
					else
							var_cut_default=${var_full#*"default"}  # cut "default"
							var_cut_equal=${var_cut_default#*"="}   # cut "="
							var_cut_bracket_origin=${var_cut_equal%]*}     # cut "]"
							var_cut_left_blank=${var_cut_bracket_origin#*" "}     # cut left blank
							var_cut_right_blank=${var_cut_left_blank%*}     # cut right blank
							var_cut_bracket=$var_cut_right_blank # rename the final default parameter type as var_cut_bracket 
					fi
					rm -rf $current_path/common/"$ID_B_BIN".txt
					sed -i "$((plugin_kernel_optional_offset_C30+poi+prid+boid+brid)) s/^/$(echo "\
	$ID_R_BIN $ID_B_BIN = $var_cut_bracket;")\n/"                                                                                 $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+1)) s/^/$(echo "\
	ge::AttrValue "$ID_B_BIN"AttrValue;")\n/"                                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+2)) s/^/$(echo "\
	if ((ge::GRAPH_SUCCESS != op.GetAttr(\""$ID_B_BIN"\", "$ID_B_BIN"AttrValue))")\n/"                                            $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+3)) s/^/$(echo "\
		|| (ge::GRAPH_SUCCESS != "$ID_B_BIN"AttrValue.GetValue<ge::AttrValue::STR>("$ID_B_BIN")))")\n/"                             $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+4)) s/^/$(echo "\
	{")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+5)) s/^/$(echo "\
		printf(\"Can not GetOpAttr "$ID_B_BIN"!\\\n\");")\n/"                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+6)) s/^/$(echo "\
	}")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
				else
					koi_enum=0
					for ID_enum in $enum_parameter
					do
						if [ "$ID_R_BIN" == "$ID_enum" ];then
							koi_enum=1
						fi
					done

					var_line_num_ori=$(grep -rn "optional $ID_R_BIN $ID_B_BIN " $current_path/common/file.txt | awk '{print $1}')
					var_line_num=${var_line_num_ori%:*}
					var_full=`sed -n "$var_line_num"p $current_path/common/file.txt` # get the sentence of $ID_B from caffe.proto
					sed -n "$var_line_num"p $current_path/common/file.txt > $current_path/common/"$ID_B_BIN".txt # get the sentence file of $ID_B from caffe.proto
					if [ `grep -c "default" $current_path/common/"$ID_B_BIN".txt` -eq '0' ];then
					    if [ "$koi_enum" == "1" ];then
							var_cut_bracket=""
				 		  else
							var_cut_bracket=0
						  fi
					else
							var_cut_default=${var_full#*"default"}  # cut "default"
							var_cut_equal=${var_cut_default#*"="}   # cut "="
							var_cut_bracket_origin=${var_cut_equal%]*}     # cut "]"
							var_cut_left_blank=${var_cut_bracket_origin#*" "}     # cut left blank
							var_cut_right_blank=${var_cut_left_blank%*}     # cut right blank
							var_cut_bracket=$var_cut_right_blank # rename the final default parameter type as var_cut_bracket 
					fi
					rm -rf $current_path/common/"$ID_B_BIN".txt

					if [ "$koi_enum" == 1 ];then
					sed -i "$((plugin_kernel_optional_offset_C30+poi+prid+boid+brid)) s/^/$(echo "\
	string $ID_B_BIN = \"$var_cut_bracket\";")\n/"                                                                                $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+1)) s/^/$(echo "\
	ge::AttrValue "$ID_B_BIN"AttrValue;")\n/"                                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+2)) s/^/$(echo "\
	if ((ge::GRAPH_SUCCESS != op.GetAttr(\""$ID_B_BIN"\", "$ID_B_BIN"AttrValue))")\n/"                                            $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+3)) s/^/$(echo "\
		|| (ge::GRAPH_SUCCESS != "$ID_B_BIN"AttrValue.GetValue<ge::AttrValue::STR>("$ID_B_BIN")))")\n/"                             $dir_op_name/plugin/$op_name"_parser_C30.cpp"	
          sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+4)) s/^/$(echo "\
	{")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+5)) s/^/$(echo "\
			printf(\"Can not GetOpAttr "$ID_B_BIN"!\\\n\");")\n/"                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+6)) s/^/$(echo "\
	}")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
          elif [[ "$ID_R_BIN" == "int32" ]]||[[ "$ID_R_BIN" == "uint32" ]]||[[ "$ID_R_BIN" == "int64" ]]||[[ "$ID_R_BIN" == "uint64" ]]||[[ "$ID_R_BIN" == "int" ]];then
					sed -i "$((plugin_kernel_optional_offset_C30+poi+prid+boid+brid)) s/^/$(echo "\
	$ID_R_BIN $ID_B_BIN = $var_cut_bracket;")\n/"                                                                                 $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+1)) s/^/$(echo "\
	ge::AttrValue "$ID_B_BIN"AttrValue;")\n/"                                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+2)) s/^/$(echo "\
	if ((ge::GRAPH_SUCCESS != op.GetAttr(\""$ID_B_BIN"\", "$ID_B_BIN"AttrValue))")\n/"                                            $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+3)) s/^/$(echo "\
		|| (ge::GRAPH_SUCCESS != "$ID_B_BIN"AttrValue.GetValue<ge::AttrValue::INT>("$ID_B_BIN")))")\n/"                             $dir_op_name/plugin/$op_name"_parser_C30.cpp"	
          sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+4)) s/^/$(echo "\
	{")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+5)) s/^/$(echo "\
			printf(\"Can not GetOpAttr "$ID_B_BIN"!\\\n\");")\n/"                                                                     $dir_op_name/plugin/$op_name"_parser_C30.cpp"
					sed -i "$(($plugin_kernel_optional_offset_C30+$poi+$prid+$boid+$brid+6)) s/^/$(echo "\
	}")\n/"                                                                                                                       $dir_op_name/plugin/$op_name"_parser_C30.cpp"
          else
              layer_param_ko_num=$(($layer_param_ko_num+1))    
          fi
				fi
			fi
		fi
		id_r_bin=$(($id_r_bin+5))
	done
	id_b_bin=$(($id_b_bin+5))
	koid=$(($koid+7))
done
koid=$(($koid-$layer_param_ko_num*7))